/**
 * 
 */
package simulation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import network.IPPacket;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;

/**
 * @author chevy
 *
 */
public class Simulator {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext container = 
				new ClassPathXmlApplicationContext("classpath:routing.xml");
		
		 long startTime = 0, endTime = 0;
		try {
			InputStream fileName = Thread.currentThread().getContextClassLoader().getResource("data.txt").openStream();
			Scanner scan = new Scanner(fileName);
			
			startTime = System.currentTimeMillis();
			while(scan.hasNext()){
				String currentLine = scan.nextLine();
				String[] params = currentLine.split(";");
				IPPacket packet = new IPPacket(params[1], params[0], "Una pruebita");
				MessageChannel channel = container.getBean("channel"+params[0], MessageChannel.class);
				channel.send(MessageBuilder.withPayload(packet).build());
			}
			endTime = System.currentTimeMillis();
			scan.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long totalTime = endTime - startTime;
		System.out.println("Se demoró: " + totalTime);
	}
	

}
